package com.sinosoft.tcmemr.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sinosoft.tcmemr.dao.DiagnoseEntityMapper;
import com.sinosoft.tcmemr.dao.PatientCaseEntityMapper;
import com.sinosoft.tcmemr.entity.DiagnoseEntity;
import com.sinosoft.tcmemr.entity.PatientCaseEntity;
import com.sinosoft.tcmemr.manager.PatientCaseManager;
import com.sinosoft.tcmemr.system.ZYEMRConstants;

@Component
public class PatientCaseManagerImpl implements PatientCaseManager{
	@Autowired
	private PatientCaseEntityMapper patiencasemapper;
	
	@Autowired
	private DiagnoseEntityMapper diagnosemapper;
	
	/**
     * 增加病历记录
     * @param record
     * @return
     */
    public boolean insertSelective(PatientCaseEntity patientcase){
    	if ( patientcase == null ){
    		return false;
    	}
    	//设定病历创建时间
    	patientcase.setClinicDate(new Date());
    	patiencasemapper.insertSelective(patientcase);
    	int iNewPcaseID = patientcase.getId();
    	List<DiagnoseEntity> diagnoseList = patientcase.getDiagnoseList();
    	if ( diagnoseList != null ){
    		for(int i=0;i < diagnoseList.size(); i++ ){
    			DiagnoseEntity diagnose = diagnoseList.get(i);
    			diagnose.setPcaseId(iNewPcaseID);
    			diagnosemapper.insertSelective(diagnose);
    		}
    	}
    	return true;
    }
    
    /**
     * 增加病历
     * @param patientcase 病历信息
     * @param doctorID  医生ID
     * @param diagnoseList  诊断列表
     * @return  true/false
     */
    public boolean addPatientCase(PatientCaseEntity patientcase,
    		Integer doctorID,String drName,List<DiagnoseEntity> diagnoseList){
    	if ( patientcase == null || doctorID == null ){
    		return false;
    	}
    	//设定病历创建时间
    	patientcase.setClinicDate(new Date());
    	patientcase.setDoctorID(doctorID);
    	patientcase.setDrName(drName);
    	patiencasemapper.insertSelective(patientcase);
    	int iNewPcaseID = patientcase.getId();
    	if ( diagnoseList != null ){
    		for(int i=0;i < diagnoseList.size(); i++ ){
    			DiagnoseEntity diagnose = diagnoseList.get(i);
    			diagnose.setPcaseId(iNewPcaseID);
    			diagnosemapper.insertSelective(diagnose);
    		}
    	}
    	return true;
    }
    /**
     * 查看病历信息
     * @param id
     * @return
     */
    public PatientCaseEntity selectByPrimaryKey(Integer id){
    	return patiencasemapper.selectByPrimaryKey(id);
    }
    
    /**
     * 查看病历信息
     * @param patientid
     * @return
     */
    public List<PatientCaseEntity> selectByPatientID(Integer patientid){
    	return patiencasemapper.selectByPatientID(patientid);
    }
    
    /**
     * 更新病历信息
     * @param patientcase
     * @return
     */
    public boolean updateByPrimaryKeySelective(PatientCaseEntity patientcase){
    	if ( patientcase == null ){
    		return false;
    	}
    	patiencasemapper.updateByPrimaryKeySelective(patientcase);
    	return true;
    }
    
    /**
     * 查询该病人看同一个医生 当天的病历
     * @param patientid
     * @param drName
     * @param clinicdate
     * @return
     */
    public PatientCaseEntity selectByToday(Integer patientid, String drName,Date clinicdate ){
    	return patiencasemapper.selectByToday(patientid, drName, clinicdate);
    }
    
    /**
     * 获取该病人可修改的病历
     * @param patientid 病人ID
     * @param doctorID 医生ID
     * @return PatientCaseEntity
     */
    public PatientCaseEntity getWritePatientCase(Integer patientid, Integer doctorID ){
    	/**
    	 * 得到该病人未归档的病历
    	 * if ( is null )
    	 * 	新建一个空的
    	 */
    	PatientCaseEntity pcase = patiencasemapper.getUnArchivePatientCase(patientid,doctorID,
    			ZYEMRConstants.PATIENTCASE_UNARCHIVE);
    	if (pcase == null )
    		pcase = new PatientCaseEntity();
    	return pcase;
    }
    
    
}
